package com.hsy.directseeding.account;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.activity.DPBaseActivity;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.directseeding.R;
import com.hsy.directseeding.common.CommonBaseActivity;
import com.hsy.directseeding.common.Consts;
import com.hsy.directseeding.main.TextMainActivity;
import com.hsy.directseeding.utils.CommonUtils;
import com.hsy.directseeding.utils.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/9.
 */
public class LoginActivity extends CommonBaseActivity {
    protected TextView login;
    protected EditText userNameEdit;
    protected EditText passEdit;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            LoginGson.Result result = DPGsonUtil.fromJson(response, LoginGson.Result.class);
            CommonUtils.saveToken(LoginActivity.this, result.getToken());
            startActivity(TextMainActivity.class);
            finish();
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_login);
        setLeftButtonVisiblity(View.GONE);
        setTitle("登录");
        initView();
        final List<EditText> editText = new ArrayList<>();
        final List<String> msg = new ArrayList<>();
        editText.add(userNameEdit);
        editText.add(passEdit);
        msg.add("请输入用户名");
        msg.add("请输入密码");
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.checkLog(LoginActivity.this, editText, msg)) {

                    CommonUtils.showProgressDialog(LoginActivity.this);
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("user_name", userNameEdit.getText().toString());
                    map.put("passwd", passEdit.getText().toString());
                    HttpUtil.httpPostWithParams(LoginActivity.this, 0, Consts.Apis.POST_LOGIN, map, callBack);
                }

            }
        });
    }


    private void initView() {
        login = (TextView) findViewById(R.id.login);
        userNameEdit = (EditText) findViewById(R.id.user_name_edit);
        passEdit = (EditText) findViewById(R.id.pass_edit);
    }
}
